var expandFlag = false;
var firstLoadFlag = true;
function loadDemoTree() {
	$('#classTree').tree({
		checkbox : false, /* 是否带复选框 */
		lines : true, /* 线条 */
		method : 'post' /* 请求数据方式 */,
		url : 'platform/LantopArchiveClassController/findChildsByNodeId', /* 请求数据地址 */
		dataType : "json", /* 数据格式 */
		onClick : function(node) {
			// 树节点单击事件
			var parentNode = $('#classTree').tree('getParent',node.target);
			var parentId = parentNode.attributes.ARCHIVE_TYPE;
			if(parentId == 2){
				isArchive(node);
			}else{
				onClickRowEvent(node);
			}
		},
		loadFilter : function(data) {
			// 过滤后台返回的数据，处理不同json结构 
			if (data.data) {
				return data.data;
			} else {
				return data;
			}
		},
		formatter : function(node) {
			return '<span title="' + node.text + '">' + node.text + '</span>';
		},
		onLoadSuccess : function(_node, data) {
			if(firstLoadFlag && parent.location.pathname.indexOf('NavigationOfCreateSubOrg.jsp')>0 && data){
				var t = $(this);
				if(expandFlag && data[0]){
					var node = t.tree('find',data[0].id);
					if(data[0].state == "closed"){
						t.tree('expand',  node.target);
					}else{
						t.tree('select',node.target);
						var parentNode = t.tree('getParent',node.target);
						var parentId = parentNode.attributes.ARCHIVE_TYPE;
						if(parentId == 2){
							isArchive(node);
						}else{
							onClickRowEvent(node);
						}
						firstLoadFlag = false;
					}
				}else{
					if(data[0].attributes.PARENT_ID=="ROOT"){ // 展开根节点至档案管理
						$(data[0].children).each(function (index, d) {
							if(d.text == "档案管理"){
								t.tree('expand',  t.tree('find',d.id).target);
								expandFlag = true;
							}
						});
					}
				}
			}
		},
	});
}

// 点击的节点刷新iframe
function onClickRowEvent(node) {
	if (node.attributes.PARENT_ID == "ROOT") {
		return;
	}
	var sysId = node.attributes.SYS_ID;
	var tableId = node.attributes.ID;
	// var nodeId = node.attributes.PID;
	// 更改树新的ndeId获取方式
	var type = node.attributes.ARCHIVE_TYPE;
	// if (tableName == "" || tableName == null || tableName == -1) {
	// nodeId = node.id;
	// } else {
	// nodeId = node.attributes.PARENT_ID;
	// }
	if (type == "-1") {
		nodeId = node.attributes.PARENT_ID;
	} else {
		nodeId = node.id;
	}
	var tableName = node.attributes.TABLE_NAME;
	
	var nodeName = node.attributes.NODE_NAME;

	// 判断点击节点是否为物理表节点
	if (tableName == "" || tableName == null || tableName == -1) {// 不是
		document.getElementById("mainIframe").src = "";
	} else {// 是
		document.getElementById("mainIframe").src = "avicit/cbb/techclass/billdefine/billdefine_manager.jsp?sysId="
				+ sysId
				+ "&tableId="
				+ tableId
				+ "&nodeId="
				+ nodeId
				+ "&tableName=" + tableName + "&nodeName=" + nodeName;
	}

}


// Create by Tangkuns
//档案类型
function isArchive(node) {
	var type = node.attributes.ARCHIVE_TYPE;
	var sysId = node.attributes.SYS_ID;
	var tableId = node.attributes.ID;
	if (type == "-1") {
		nodeId = node.attributes.PARENT_ID;
	} else {
		nodeId = node.id;
	}
	var tableName = node.attributes.TABLE_NAME;
	var nodeName = node.attributes.NODE_NAME;
	
	if (tableName == "" || tableName == null || tableName == -1) {// 不是
		document.getElementById("mainIframe").src = "";
	} else {// 是
		document.getElementById("mainIframe").src = "avicit/cbb/techclass/billdefine/billdefine_archive_manager.jsp?sysId="
				+ sysId
				+ "&tableId="
				+ tableId
				+ "&nodeId="
				+ nodeId
				+ "&tableName=" + tableName + "&nodeName=" + nodeName;
	}
}